use sqlx::Row;
use anyhow::{anyhow, Result};
use crate::db::db_init::db_init;
use super::Config;

pub async fn db_select(key: &str) -> Result<Config> {
    let db_pool = db_init().await;
    let row = sqlx::query("select * from config where key = ?")
        .bind(key)
        .fetch_optional(&*db_pool)
        .await?;

    match row {
        Some(r) => {
            return Ok(Config { key: r.get("key"), value: r.get("value") });
        }
        None => {
            return Err(anyhow!("could not find key in db"));
        }
    }
}
